//
//  AKBottomLineTextField.m
//  AirKabClient
//
//  Created by 张可可 on 2017/4/11.
//  Copyright © 2017年 airkab. All rights reserved.
//

#import "AKBottomLineTextField.h"

@interface AKBottomLineTextField ()

@property (nonatomic, strong) CALayer *bottomLineLayer;

@end

@implementation AKBottomLineTextField

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setup];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    self.borderStyle = UITextBorderStyleNone;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(bottomLineTextFieldStartEditNotification:) name:UITextFieldTextDidBeginEditingNotification object:self];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(bottomLineTextFieldEndEditNotification:) name:UITextFieldTextDidEndEditingNotification object:self];
}

- (void)bottomLineTextFieldStartEditNotification:(NSNotification *)ntf {
    self.bottomLineLayer.backgroundColor = UIColor.k_t1Color.CGColor;
}
- (void)bottomLineTextFieldEndEditNotification:(NSNotification *)ntf {
    self.bottomLineLayer.backgroundColor = global_view_darkLineColor.CGColor;
}


- (void)setup {
    self.bottomLineLayer = [CALayer layer];
    self.bottomLineLayer.backgroundColor = global_view_darkLineColor.CGColor;
    
    [self.layer addSublayer:self.bottomLineLayer];
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.bottomLineLayer.frame = CGRectMake(0, self.height-0.5, self.width, 0.5);
}

- (void)setLeftImageViewWithImageName:(NSString *)imageName {
    
    UIView *leftView = [[UIView alloc]  initWithFrame:CGRectMake(0, 0, 40, 40)];
    UIImageView *leftImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    [leftView addSubview:leftImageView];
    leftImageView.frame = CGRectMake(0, 0, 17, 20);
    leftImageView.center = leftView.center;
    self.leftView = leftView;
    self.leftViewMode = UITextFieldViewModeAlways;
}

@end
